package com.jarchie.htgl.utils;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.IBinder;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.coder.zzq.smartshow.toast.SmartToast;
import com.jarchie.htgl.R;
import com.jarchie.htgl.activity.MainActivity;
import com.jarchie.htgl.constants.ConfigConstant;
import com.jarchie.htgl.views.SpinnerPopWindow;

/**
 * Created by Jarchie on 2017\12\11.
 * 创建常用的工具方法
 */

@SuppressWarnings({"WeakerAccess", "deprecation"})
public class CommonUtils {

    private static long exitTime = 0; //退出APP的时间

    //双击退出整个APP
    public static void exitApp(MainActivity context) {
        if (!BackHandlerHelper.handleBackPress(context)) {
            if ((System.currentTimeMillis() - exitTime) > ConfigConstant.EXIT_TIME) {
                SmartToast.show("再按一次退出程序");
                exitTime = System.currentTimeMillis();
            } else {
                context.finish();
                //结束进程
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(0);
            }
        }
    }

    //设置字体
    public static void setFont(Context mContext, TextView textView) {
        Typeface fontType = Typeface.createFromAsset(mContext.getAssets(), "fonts/FONT.TTF");
        textView.setTypeface(fontType);
    }

    //给TextView右边设置图片,这里用于PopupWindow中
    public static void setTextUPImage(Context context, SpinnerPopWindow spiner, TextView textView) {
        spiner.setWidth(textView.getWidth());
        spiner.showAsDropDown(textView);
        Drawable drawable = context.getResources().getDrawable(R.drawable.icon_up);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());// 必须设置图片大小，否则不显示
        textView.setCompoundDrawables(null, null, drawable, null);
    }

    public static void setDownImage(Context context, TextView textView) {
        Drawable drawable = context.getResources().getDrawable(R.drawable.icon_down);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());// 必须设置图片大小，否则不显示
        textView.setCompoundDrawables(null, null, drawable, null);
    }

    //关闭应用程序，杀死当前进程
    public static void killApp() {
        ActivityManager.getInstance().finishAllActivity();
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

    //判断是否需要隐藏
    @SuppressWarnings("RedundantIfStatement")
    public static boolean isHideInput(View view, MotionEvent event) {
        if (view != null && (view instanceof EditText)) {
            int[] loc = {0, 0};
            view.getLocationInWindow(loc);
            int left = loc[0], top = loc[1], bottom = top + view.getHeight(), right = left + view.getWidth();
            if (event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    //隐藏软键盘
    public static void hideSoftInput(Context context, IBinder binder) {
        if (binder != null) {
            InputMethodManager manager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(binder, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 软键盘将布局弹起
     *
     * @param layout 根布局
     * @param scroll 最下方View
     */
    public static void addLayoutListener(final View layout, final View scroll) {
        layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //获取根视图在窗体的可视区域
                Rect rect = new Rect();
                layout.getWindowVisibleDisplayFrame(rect);
                //获取根视图在窗体的不可视区域
                int layoutInvisiableHeight = layout.getRootView().getHeight() - rect.bottom;
                //判断不可视区域高度，大于150键盘显示，小于150键盘隐藏
                if (layoutInvisiableHeight > 150) {
                    //获取Scroll的窗体坐标
                    int[] location = new int[2];
                    scroll.getLocationInWindow(location);
                    //算出根视图需要滚动的高度，使scroll显示
                    int scrollHeight = (location[1] + scroll.getHeight()) - rect.bottom;
                    if (scrollHeight > 0) {
                        layout.scrollTo(0, scrollHeight);
                    }
                } else {
                    layout.scrollTo(0, 0);
                }
            }
        });
    }

}
